package com.vitas.coin.audio;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class Audio {
    public static List<AudioData> loadAudio(Context context, int page, int pageSize) {
        List<AudioData> audios = new ArrayList<>();
        audios.clear();
        final String[] videoProjection = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATE_MODIFIED,
                MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST};
        Cursor cursor = null;
        try {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
                Bundle bundle = new Bundle();
                bundle.putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, videoProjection[4] + " DESC");
                bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, pageSize);
                bundle.putInt(ContentResolver.QUERY_ARG_OFFSET, page * pageSize);
                cursor = context.getContentResolver()
                        .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, videoProjection, bundle, null);
            } else {
                cursor = context.getContentResolver()
                        .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, videoProjection, null, null,
                                videoProjection[4] + " DESC LIMIT " + page * pageSize + " , " + pageSize);
            }
        } catch (SecurityException e) {
            Log.e("tagg", e.getMessage());
        }
        try {
            while (cursor != null && cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndexOrThrow(videoProjection[1]));
                String path = cursor.getString(cursor.getColumnIndexOrThrow(videoProjection[2]));
                long size = cursor.getLong(cursor.getColumnIndexOrThrow(videoProjection[3]));
                long addTime = cursor.getLong(cursor.getColumnIndexOrThrow(videoProjection[4]));
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(videoProjection[5]));
//                int errorCode = CodecUtil.verificationAudioFormat(path);
//                if (errorCode != HVEErrorCode.SUCCESS) {
//                    SmartLog.w(TAG, "verificationAudio return " + errorCode);
//                    continue;
//                }

                AudioData item = new AudioData();
                item.setName(name);
                item.setPath(path);
                item.setSize(size);
                item.setAddTime(addTime);
                item.setDuration(duration);
                audios.add(item);
            }
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        } catch (RuntimeException e) {
            Log.e("tagg", e.getMessage());
        }

        return audios;
    }
}
